package com.huang.gulimail.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huang.common.constant.ProductConstant;
import com.huang.gulimail.product.dao.AttrAttrgroupRelationDao;
import com.huang.gulimail.product.dao.AttrGroupDao;
import com.huang.gulimail.product.dao.CategoryDao;
import com.huang.gulimail.product.entity.*;
import com.huang.gulimail.product.service.CategoryService;
import com.huang.gulimail.product.vo.AttrGroupRelationVo;
import com.huang.gulimail.product.vo.AttrRespVo;
import com.huang.gulimail.product.vo.AttrVo;
//import com.sun.xml.internal.bind.v2.TODO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huang.common.utils.PageUtils;
import com.huang.common.utils.Query;

import com.huang.gulimail.product.dao.AttrDao;
import com.huang.gulimail.product.entity.AttrEntity;
import com.huang.gulimail.product.service.AttrService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Autowired
    AttrAttrgroupRelationDao attrAttrgroupRelationDao;
    @Autowired
    AttrGroupDao attrGroupDao;
    @Autowired
    CategoryDao categoryDao;
    @Autowired
    CategoryService categoryService;
    @Autowired
    AttrDao attrDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId, String attrType) {
        String key=(String)params.get("key");
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<AttrEntity>();
        wrapper.eq("attr_type","base".equalsIgnoreCase(attrType)?1:0);
        if(StringUtils.hasText(key)){

//                wrapper.and(obj->{
//                    obj.eq("attr_group_id",key).or().like("attr_group_name",key);
//                });
            wrapper.eq("attr_id",key).or().like("attr_name",key);

        }
        if(catelogId!=0){
            wrapper.eq("catelog_id",catelogId);
        }
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                wrapper
        );
        List<AttrEntity> records = page.getRecords();
        List<AttrRespVo> respVoList=records.stream().map((attrEntity)->{
            AttrRespVo attrRespVo = new AttrRespVo();
            BeanUtils.copyProperties(attrEntity,attrRespVo);
            AttrAttrgroupRelationEntity attrId=attrAttrgroupRelationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id",attrEntity.getAttrId()));
            if(attrId!=null) {
                AttrGroupEntity attrGroupId = attrGroupDao.selectById(attrId.getAttrGroupId());
                if(attrGroupId!=null) {
                    attrRespVo.setGroupName(attrGroupId.getAttrGroupName());
                }
            }
            CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId());
            if(categoryEntity!=null){
                attrRespVo.setCatelogName(categoryEntity.getName());
            }
            return attrRespVo;
        }).collect(Collectors.toList());
        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(respVoList);

        return pageUtils;
    }


    @Override
    @Transactional
    public void saveAttr(AttrVo attr) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attr,attrEntity);
        this.save(attrEntity);
        if(attr.getAttrType()== ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()){
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();

            attrAttrgroupRelationEntity.setAttrGroupId(attr.getAttrGroupId());
            attrAttrgroupRelationEntity.setAttrId(attrEntity.getAttrId());
            attrAttrgroupRelationDao.insert(attrAttrgroupRelationEntity);
        }


    }

    @Override
    public AttrRespVo getAttrInfo(Long attrId) {
        AttrRespVo attrRespVo = new AttrRespVo();
        AttrEntity attrEntity=this.getById(attrId);
        BeanUtils.copyProperties(attrEntity,attrRespVo);
        AttrAttrgroupRelationEntity relationEntity = attrAttrgroupRelationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrId));
        if(relationEntity!=null){
        attrRespVo.setAttrGroupId(relationEntity.getAttrGroupId());
        AttrGroupEntity attrGroupEntity=attrGroupDao.selectById(relationEntity.getAttrGroupId());
        if(attrGroupEntity!=null){
        attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
        }
        }
        Long catelogId=attrEntity.getCatelogId();
        Long[] catelogPath=categoryService.findCatelogPath(catelogId);
        attrRespVo.setCatelogPath(catelogPath);
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        if(categoryEntity!=null){
            attrRespVo.setCatelogName(categoryEntity.getName());
        }

        return attrRespVo;
    }

    @Override
    @Transactional
    public void updateAttr(AttrVo attr) {
        AttrEntity attrEntity=new AttrEntity();
        BeanUtils.copyProperties(attr,attrEntity);
        this.updateById(attrEntity);
        if(attr.getAttrType()== ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()) {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            relationEntity.setAttrGroupId(attr.getAttrGroupId());
            relationEntity.setAttrId(attr.getAttrId());
            Integer count=attrAttrgroupRelationDao.selectCount(new UpdateWrapper<AttrAttrgroupRelationEntity>().eq("attr_id",attr.getAttrId()));
            if(count>0){
                attrAttrgroupRelationDao.update(relationEntity,new UpdateWrapper<AttrAttrgroupRelationEntity>().eq("attr_id",attr.getAttrId()));
            }else {
                attrAttrgroupRelationDao.insert(relationEntity);
            }
        }

    }

    @Override
    public List<AttrEntity> getRelationAttr(Long attrgroupId) {
        List<AttrAttrgroupRelationEntity> attrgroupList= attrAttrgroupRelationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrgroupId));
        List<Long>attrIds=attrgroupList.stream().map((attr)->{
            return attr.getAttrId();
        }).collect(Collectors.toList());
        List<AttrEntity>list=new ArrayList<>();
        //attrIds.size()==0
        if(attrIds==null||attrIds.size()==0){return null;}
        System.out.println(attrIds);
           list=attrDao.selectBatchIds(attrIds);

        return list;
    }

    @Override
    public void deleteRelation(AttrGroupRelationVo[] vos) {
        //QueryWrapper<AttrAttrgroupRelationEntity> w = new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", vos[0].getAttrGroupId());
        List<AttrAttrgroupRelationEntity> collect = Arrays.stream(vos).map(vo -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(vo, relationEntity);
            return relationEntity;
        }).collect(Collectors.toList());
        attrAttrgroupRelationDao.deleteBatchRelation(collect);
    }
    @Override
    public PageUtils getNoRelationAttr(Map<String, Object> params,Long attrgroupId) {
        String key=(String)params.get("key");
        AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrgroupId);
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<AttrEntity>().eq("catelog_id", attrGroupEntity.getCatelogId());

        if(StringUtils.hasText(key)){
//                wrapper.and(obj->{
//                    obj.eq("attr_group_id",key).or().like("attr_group_name",key);
//                });
            wrapper.eq("attr_id",key).or().like("attr_name",key);
        }
        //List<AttrAttrgroupRelationEntity> attrgroupList= attrAttrgroupRelationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrgroupId));
        List<AttrAttrgroupRelationEntity> attrgroupList= attrAttrgroupRelationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>());
        List<Long>attrIds=attrgroupList.stream().map((attr)->{
            return attr.getAttrId();
        }).collect(Collectors.toList());
        //attrIds.size()==0
        if(attrIds!=null&&attrIds.size()!=0) {
            wrapper.notIn("attr_id", attrIds);
        }
            IPage<AttrEntity> page = this.page(new Page<AttrEntity>(), wrapper);
            return new PageUtils(page);
    }
}